//-----------------------------------------------------------------------------
//  Copyright (C) 2005-2008 Thomas S. Ullrich 
//
//  This file is part of the Solitaire Mahjong project.
//
//  This file may be used under the terms of the GNU General Public License.
//  This project is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License.
//  
//  Author: Thomas S. Ullrich
//  Last update: November 14, 2008
//-----------------------------------------------------------------------------
#include <QApplication>
#include <QDir>
#include <QLayout>
#include <QPushButton>
#include <QTextBrowser>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QtDebug>
#include "smHelpBrowser.h"
#include "smSettings.h"
 
smHelpBrowser::smHelpBrowser(const QString &path, const QString &page,
                             QWidget *parent)	: QWidget(parent)
{
    setAttribute(Qt::WA_DeleteOnClose);
	setWindowModality(Qt::NonModal);
	 
    mTextBrowser = new QTextBrowser(this);
    mHomeButton  = new QPushButton(tr("&Home"), this);
    mBackButton  = new QPushButton(tr("&Back"), this);
    mCloseButton = new QPushButton(tr("&Close"), this);
 
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setMargin(3);
 
    QHBoxLayout *buttonLayout = new QHBoxLayout; //(mainLayout);
	mainLayout->addLayout(buttonLayout); // new, works?
    buttonLayout->setMargin(3);
    buttonLayout->addWidget(mHomeButton);
    buttonLayout->addWidget(mBackButton);
    buttonLayout->addStretch(3);
    buttonLayout->addWidget(mCloseButton);
    mainLayout->addWidget(mTextBrowser);
    
    setWindowIcon(QIcon(QString::fromUtf8(":/images/images/logo32.png")));

 
    connect(mHomeButton, SIGNAL(clicked()), mTextBrowser, SLOT(home()));
    connect(mBackButton, SIGNAL(clicked()), mTextBrowser, SLOT(backward()));
    connect(mCloseButton, SIGNAL(clicked()), this, SLOT(close()));
    connect(mTextBrowser, SIGNAL(sourceChanged(const QUrl&)), this, SLOT(updateCaption()));
 
    mTextBrowser->setSearchPaths(QStringList(path));
    mTextBrowser->setSource(page);
    
    move(smSettings::instance()->mHelpBrowserPosition);
    resize(700,650);
}
 
void smHelpBrowser::updateCaption()
{
    setWindowTitle(tr("Help: %1").arg(mTextBrowser->documentTitle()));
}
 
void smHelpBrowser::showPage(const QString &page)
{
    QString path = qApp->applicationDirPath() + "/docs";
    QDir dir(path);
    if (!dir.exists()) {
        path = qApp->applicationDirPath() + "/../docs";
        dir.setPath(path);
    }
#if defined(Q_OS_MAC)   
    if (!dir.exists()) {
        path = qApp->applicationDirPath() + "/../Resources/docs";
        dir.setPath(path);
    }
#endif
    if (!dir.exists()) {
        QMessageBox::warning( 0, "Solitaire Mahjong",
                              "Cannot find the directory holding the documentation (docs).\n"
                              "Solitaire Mahjong will still run but you won't be able to use the\n"
                              "help utility. Check your installation and reinstall if necessary.");
    }
    smHelpBrowser *browser = new smHelpBrowser(path, page);
    browser->show();
}

void smHelpBrowser::hideEvent(QHideEvent*)
{
    smSettings::instance()->mHelpBrowserPosition = pos();   
}


void smHelpBrowser::showEvent(QShowEvent*)
{
    move(smSettings::instance()->mHelpBrowserPosition);
}

